<?php
/**
 * User: Nicho
 * Date: 17-3-31
 * Time: AM 08:10
 */

namespace Admin\Controller;
class PayController extends AdminController
{
    //单位表
    private $unitModel;
    //缴费表
    private $payItemModel;
    //缴费配置表
    private $payItemConfModel;
    public function __construct(){
        parent::__construct();
        $this->unitModel = M('core_unit');
        $this->payItemModel = M('core_pay_item');
        $this->payItemConfModel = M('core_pay_item_config');
        $this->payItemConfModel->create();
        $this->payItemModel->create();
    }
    const MODEL_NAME = 'Pay@:';
    //缴费列表
    public function index()
    {
        $con = "item.unit = unit.id and item.id = itemconf.item and item.status <> -1";
        if(I('keyword')){
            $keyword = I('keyword');
            $con.= " and item.title like '%".I('keyword')."%' ";
            $this->assign('keyword',$keyword);
        }
        $totalCount = $this->payItemModel->table('dkyey_core_pay_item item,dkyey_core_unit unit,dkyey_core_pay_item_config itemconf')->where($con.' and item.pid = 0')->count();
        $page = new \Think\Page($totalCount,20);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $this->payItemModel->table('dkyey_core_unit unit,dkyey_core_pay_item item,dkyey_core_pay_item_config itemconf')->where($con)->field('unit.title as utitle,item.*,itemconf.start_date,itemconf.end_date')->limit($limit)->order('item.id desc')->select();
        //缴费子项
        foreach ($list as $key => $value) {
            $list[$key]['sub'] = $this->payItemModel->where('dkyey_core_pay_item.status <> -1 and dkyey_core_pay_item.pid = '.$value['id'])->join('dkyey_core_pay_item_config conf ON dkyey_core_pay_item.id = conf.item')->field('dkyey_core_pay_item.*,conf.setstate')->order('dkyey_core_pay_item.id desc')->select();
        }
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '缴费项目列表';
        $this->display(self::MODEL_NAME.'Pay/index'); 
        
    }
    //添加/编辑缴费
    public function updateBill()
    {
        if(IS_POST){
            if(!I('post.start_date')) $this->error('请输入起始时间!');
            if(!I('post.end_date')) $this->error('请输入结束时间!');

            $this->payItemConfModel->start_date = strtotime(I('start_date'));
            $this->payItemConfModel->end_date = strtotime(I('end_date'));

            switch (I('a_type')) {
                case 'add':
                    $this->payItemModel->status = 1;
                    $this->payItemConfModel->status = 1;
                    $this->payItemModel->create_time = time();
                    $this->payItemConfModel->create_time = time();
                    $result = $this->payItemModel->add();
                    $this->payItemConfModel->item = $result;
                    $result2 = $this->payItemConfModel->add();
                    break;
                case 'edit':
                    $this->payItemModel->update_time = time();
                    $this->payItemConfModel->update_time = time();
                    $result = $this->payItemModel->where('id = '.I('iid'))->save();
                    $result2 = $this->payItemConfModel->where('item = '.I('iid'))->save();
                    break;
            }
            if($result&&$result2){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            //单位列表
            $unitList = $this->unitModel->where('status = 1')->select();
            $this->assign('unitList',$unitList);
            //为编辑使用
            $item = $this->payItemModel->where('id = '.I('id'))->find();
            $tmp = $this->payItemConfModel->where('item = '.I('id'))->find();
            //格式化时间
            $item['start_date'] = date('Y-m-d H:i:s',$tmp['start_date']);
            $item['end_date'] = date('Y-m-d H:i:s',$tmp['end_date']);

            $this->assign('data',$item);
            //判断添加/编辑
            $this->assign('a_type',I('a_type'));
            $this->display(self::MODEL_NAME.'Pay/updateBill');
        }
    }
    //添加/编辑缴费
    public function updatePay()
    {
        if(IS_POST){
            if(!I('post.start_date')) $this->error('请输入起始时间!');
            if(!I('post.end_date')) $this->error('请输入结束时间!');

            $this->payItemConfModel->start_date = strtotime(I('start_date'));
            $this->payItemConfModel->end_date = strtotime(I('end_date'));

            switch (I('a_type')) {
                case 'add':
                    $this->payItemModel->status = 1;
                    $this->payItemConfModel->status = 1;
                    $this->payItemModel->create_time = time();
                    $this->payItemConfModel->create_time = time();
                    $result = $this->payItemModel->add();
                    $this->payItemConfModel->item = $result;
                    $result2 = $this->payItemConfModel->add();
                    break;
                case 'edit':
                    $this->payItemModel->update_time = time();
                    $this->payItemConfModel->update_time = time();
                    $result = $this->payItemModel->where('id = '.I('iid'))->save();
                    $result2 = $this->payItemConfModel->where('item = '.I('iid'))->save();
                    break;
            }
            if($result&&$result2){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            //单位列表
            $unitList = $this->unitModel->where('status = 1')->select();
            $this->assign('unitList',$unitList);
            //为编辑使用
            $item = $this->payItemModel->where('id = '.I('id'))->find();
            $tmp = $this->payItemConfModel->where('item = '.I('id'))->find();
            //格式化时间
            $item['start_date'] = date('Y-m-d H:i:s',$tmp['start_date']);
            $item['end_date'] = date('Y-m-d H:i:s',$tmp['end_date']);

            $this->assign('data',$item);
            //判断添加/编辑
            $this->assign('a_type',I('a_type'));
            $this->display(self::MODEL_NAME.'Pay/updatePay');
        }
    }
    //添加/编辑缴费子项
    public function updateSubPay()
    {
        if(IS_POST){
            switch (I('a_type')) {
                case 'add':
                    $this->payItemConfModel->status = 1;
                    $this->payItemModel->status = 1;
                    $this->payItemConfModel->create_time = time();
                    $this->payItemModel->create_time = time();
                    $result = $this->payItemModel->add();
                    $this->payItemConfModel->item = $result;
                    $result2 = $this->payItemConfModel->add();
                    break;
                case 'edit':
                    $this->payItemModel->update_time = time();
                    $this->payItemConfModel->update_time = time();
                    $this->payItemConfModel->item = I('id');
                    $result = $this->payItemModel->where('id = '.I('id'))->save();
                    $result2 = $this->payItemConfModel->where('item = '.I('id'))->save();
                    break;
            }
            if($result&&$result2){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            //为编辑使用
            $iConf = $this->payItemConfModel->where('id = '.I('id'))->find();
            $this->assign('data',$iConf);
            //判断添加/编辑
            $this->assign('pid',I('pid'));          //母项id，添加时使用
            $this->assign('ititle',I('ititle'));    //母项名称，添加时使用
            $this->assign('a_type',I('a_type'));    //添加、编辑判断
            $this->display(self::MODEL_NAME.'Pay/updateSubPay');
        }
    }
    //缴费配置
    public function payConf()
    {
        if(IS_POST){
            if(I('id')){
                $this->payItemConfModel->update_time = time();
                $result = $this->payItemConfModel->where('id = '.I('id'))->save();
            }else{
                $this->payItemConfModel->create_time = time();
                $result = $this->payItemConfModel->add();
            }
            if($result){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            $payConf = $this->payItemConfModel->where('item = '.I('id'))->find();
           
            $this->assign('data',$payConf);
            $this->assign('tid',I('id'));
            $this->assign('utitle',I('utitle'));
            $this->display(self::MODEL_NAME.'pay/payConf');
        }
    }
     public function bill()
    {
        $con = "item.unit = unit.id and item.id = itemconf.item and item.status <> -1";
        if(I('keyword')){
            $keyword = I('keyword');
            $con.= " and item.title like '%".I('keyword')."%' ";
            $this->assign('keyword',$keyword);
        }
        $totalCount = $this->payItemModel->table('dkyey_core_pay_item item,dkyey_core_unit unit,dkyey_core_pay_item_config itemconf')->where($con.' and item.pid = 0')->count();
        $page = new \Think\Page($totalCount,20);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $this->payItemModel->table('dkyey_core_unit unit,dkyey_core_pay_item item,dkyey_core_pay_item_config itemconf')->where($con)->field('unit.title as utitle,item.*,itemconf.start_date,itemconf.end_date')->limit($limit)->order('item.id desc')->select();
        //缴费子项
        foreach ($list as $key => $value) {
            $list[$key]['sub'] = $this->payItemModel->where('dkyey_core_pay_item.status <> -1 and dkyey_core_pay_item.pid = '.$value['id'])->join('dkyey_core_pay_item_config conf ON dkyey_core_pay_item.id = conf.item')->field('dkyey_core_pay_item.*,conf.setstate')->order('dkyey_core_pay_item.id desc')->select();
        }
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '缴费项目列表';
        $this->display(self::MODEL_NAME.'Pay/bill'); 
        
    }

    //设置状态
     public function setStatus()
    {
        $ids = I('request.id');
        $model = I('request.model');
        if(!$ids)$this->error('无可操作数据!');
        $map['id'] = array('in',$ids);
        $method = I('request.method');
        $model_object = D($model);
        switch ($method) {
            case 'resume':
                $model_object->status = 1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('激活成功!');
                }else{
                    $this->error('激活失败!');
                }
                break;
            case 'forbid':
                $model_object->status = 2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('禁用成功!');
                }else{
                    $this->error('禁用失败!');
                }
                break;
            case 'delete':
                $model_object->status = -1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('删除成功!');
                }else{
                    $this->error('删除失败!');
                }
                break;
            case 'recover':
                $model_object->status = 0;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('恢复成功!');
                }else{
                    $this->error('恢复失败!');
                }
                break;
            case 'destory':
                $model_object->status = -2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('彻底删除成功!');
                }else{
                    $this->error('彻底删除失败!');
                }
                break;
            default:
                $this->error('系统参数错误!');
                break;
        }
    }
}
